/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package unilearn.dao.hibernate;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import unilearn.dao.PersonRoleDAO;
import unilearn.vo.PersonRole;

/**
 *
 * @author Yat
 */
public class PersonRoleDAOImpl extends HibernateDAO implements PersonRoleDAO {

    public PersonRoleDAOImpl() {
        super();
    }

    public PersonRoleDAOImpl(SessionFactory sessionFactory) {
        super(sessionFactory);
    }

    @Override
    public void setSessionFactory(SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
    }

    @Override
    public List<PersonRole> getAll() throws Exception {
        return super.getAll(PersonRole.class.getName());
    }

    @Override
    public PersonRole get(int id) throws Exception {
        Session s = sessionFactory.openSession();
        Transaction t = s.beginTransaction();

        try {
            Query query = s.createQuery("from " + PersonRole.class.getName() + " pr where pr.id = " + id);
            List<PersonRole> prs = query.list();
            if ((prs == null) || (prs.isEmpty())) {
                return null;
            }

            return prs.get(0);
        } finally {
            t.commit();
            s.close();
        }
    }

    @Override
    public List<PersonRole> find(PersonRole personRole) throws Exception {
        return (List<PersonRole>) super.find(personRole);
    }

    @Override
    public PersonRole insert(PersonRole personRole) throws Exception {
        super.insert(personRole);
        return personRole;
    }

    @Override
    public PersonRole update(PersonRole personRole) throws Exception {
        super.update(personRole);
        return personRole;
    }

    @Override
    public void delete(PersonRole personRole) throws Exception {
        super.delete(personRole);
    }
}
